class status{

    url =  ''
    back = false;
    data = {}
    history_url = [];
    constructor(url){
        this.url = url

        if (window.performance && typeof window.performance.getEntries =='function' && window.performance.getEntries()[0].type == 'back_forward'){
            this.back = true;    
        }

        if ( sessionStorage.getItem('admin_back')){
            this.back = true;
            sessionStorage.removeItem('admin_back');
        }

        if (sessionStorage.getItem('admin_status_data')){
            this.data = JSON.parse(sessionStorage.getItem('admin_status_data'));
        }

        let admin_history_url = [];
        
        if (sessionStorage.getItem('admin_history_url')){
            admin_history_url = JSON.parse(sessionStorage.getItem('admin_history_url'));
            this.history_url =  JSON.parse(sessionStorage.getItem('admin_history_url'));
        }

        if (admin_history_url[admin_history_url.length-1] != url) admin_history_url.push(url);

        //只保留后面100个元素
        if (admin_history_url.length > 100) admin_history_url.shift();


        sessionStorage.setItem('admin_history_url',JSON.stringify(admin_history_url));
    }

    get(key){
        if (this.back){
            if (this.data[this.url]){
                return this.data[this.url][key]??null;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    set(key,data){
        if (!this.data[this.url]){
            this.data[this.url] = {};
        }

        this.data[this.url][key] = data;
        this.save();
    }

    setData(data){
        this.data[this.url] = data;
        this.save();
    }

    setBack(){
        sessionStorage.setItem('admin_back',this.url);
    }

    setbackData(key,value){
        let url = ''
        if (this.history_url.length > 0){
            url =  this.history_url[this.history_url.length - 1];
        }else{
            return ;
        }

        if (this.data[url]){
            this.data[url][key] = value;
        }else{
            this.data[url] = {};
            this.data[url][key] = value;
        }

        this.save();
    }

    save(){
        sessionStorage.setItem('admin_status_data',JSON.stringify(this.data));
    }

}

export default status;